package com.zh.yida.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.yida.common.BusinessException;
import com.zh.yida.common.R;
import com.zh.yida.dto.SetmealDto;
import com.zh.yida.entity.Setmeal;
import com.zh.yida.service.CategoryService;
import com.zh.yida.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private SetmealService setMealService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 保存套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setMealService.saveDishWithSetmeal(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 查询套餐分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = setMealService.selectSetmeal(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        setMealService.page(setmealPage, lqw);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new LinkedList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            String categoryName = categoryService.getById(setmeal.getCategoryId()).getName();
            setmealDto.setCategoryName(categoryName);
            setmealDtoList.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    /**
     * 查询单个套餐回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealDto(@PathVariable Long id) {
        SetmealDto setmealDto = setMealService.getSetmealDto(id);
        Long categoryId = setmealDto.getCategoryId();
        setmealDto.setCategoryName(categoryService.getById(categoryId).getName());
        return R.success(setmealDto);
    }

    /**
     * 更新套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> updateSetmealDto(@RequestBody SetmealDto setmealDto) {
        setMealService.updateSetmealDto(setmealDto);
        return R.success("更新成功");
    }

    /**
     * 修改套餐售卖状况，0：禁用， 1：启用
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        if (ids == null) {
            throw new BusinessException("请选择要修改的套餐");
        } else {
            for (long id : ids) {
                Setmeal setmeal = setMealService.getById(id);
                setmeal.setStatus(status);
                setMealService.updateById(setmeal);
            }
        }
        return R.success("修改成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    //allEntries = true 表示删除该组下所有缓存
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> DeleteSetmealDto(@RequestParam List<Long> ids) {
        if (ids == null) {
            throw new BusinessException("请选择要删除的套餐");
        } else {
            setMealService.deleteSetmealDto(ids);
        }
        return R.success("删除成功");
    }

    /**
     * 查询单个套餐的菜品
     * @param
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status", unless = "#setmeal == null")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setMealService.list(lqw);
        return R.success(list);
    }
}
